package cn.itcast.service.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;
    @Override
    public void save(ExtCproduct extCproduct) {
        // 1 设置一些附件的额外属性（uuid 总金额...）
        extCproduct.setId(UUID.randomUUID().toString());
        //附件的总金额==附件的单价*附件的数量
        Double total=extCproduct.getPrice()*extCproduct.getCnumber();
        extCproduct.setAmount(total);
        //2 保存附件的数据到数据库
        extCproductDao.insertSelective(extCproduct);
        //3 根据附件的所属合同id查询合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //4 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()+total);
        //5 修改合同的附件数
        contract.setExtNum(contract.getExtNum()+1);
        //6 修改合同数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        /*根据附件的id获取到以前的附件对象*/
        ExtCproduct extCproduct1 = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        /*获取附件以前的总金额*/
        Double old_total = extCproduct1.getAmount();
        //计算附件修改之后的总金额
        Double new_total=extCproduct.getPrice()*extCproduct.getCnumber();
        extCproduct.setAmount(new_total);
        //修改附件数据到数据库
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
        /*根据附件的合同id获取到合同*/
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
       // 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-old_total+new_total);
        //修改合同数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);
    }
    @Override
    public void delete(String id) {
        //1 获取到附件对象
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        //2 获取附件以前的总金额
        Double old_total = extCproduct.getAmount();
        //3 删除附件
        extCproductDao.deleteByPrimaryKey(id);
        // 根据附件的合同id获取到合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        /*设置合同总金额*/
        contract.setTotalAmount(contract.getTotalAmount()-old_total);
        /*设置合同的总数量*/
        contract.setExtNum(contract.getExtNum()-1);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ExtCproduct findById(String id) {
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        return extCproduct;
    }
    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(example);
        PageInfo pageInfo=new PageInfo(extCproducts);
        return pageInfo;
    }

}
